'''
@author: saaj
@license: LGPL
'''


import os.path
import cherrypy
import config
import MySQLdb as mysql
from . import controller


class Bootstrap:
  
  config = None
  path   = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))


  def __init__(self):
    environment = "development"
    if "environment" in cherrypy.config:
      environment = cherrypy.config["environment"]
    # allow static access as well
    Bootstrap.config = getattr(config, environment)
    
    cherrypy.config.update(self.config)
    cherrypy.tree.mount(controller.Root(), "/", self.config)
    
    cherrypy.engine.subscribe("start_thread", self._onThreadStart)
    
    self._onThreadStart(-1)

  def _onThreadStart(self, threadIndex):
    cherrypy.thread_data.db   = mysql.connect(**self.config["database"])
    cherrypy.thread_data.path = self.path